package wowraidmanager.client.auction.admin.maintain;

import java.util.ArrayList;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.auction.CheckItemListService;
import wowraidmanager.client.service.auction.CheckItemListServiceAsync;
import wowraidmanager.shared.model.auction.CheckItemList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AdminItemList extends BaseComposite{

    private static AdminItemListUiBinder uiBinder = GWT.create(AdminItemListUiBinder.class);

    protected CheckItemListServiceAsync checkItemListService = GWT.create(CheckItemListService.class);

    interface AdminItemListUiBinder extends UiBinder<Widget, AdminItemList>{
    }

    static int COLUMN_ITEMNAME = 0;
    static int COLUMN_ITEMID = 1;
    static int COLUMN_COLOR = 2;
    static int COLUMN_TYPE = 3;
    static int COLUMN_KEYHIDDEN = 4;

    @UiField
    Button addButton;

    @UiField
    Button saveButton;

    @UiField
    FlexTable checkItemListFlextable;

    ArrayList<CheckItemList> checkItemListList;

    ArrayList<CheckItemList> setCheckItemListList;

    public AdminItemList(){
        initWidget(uiBinder.createAndBindUi(this));
        setButton();
        getCheckItemList();
    }

    private void getCheckItemList(){
        checkItemListService.getCheckItemList(new AsyncCallback<ArrayList<CheckItemList>>(){

            public void onSuccess(ArrayList<CheckItemList> result){
                checkItemListList = result;
                setItemListFlexTable();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("CheckItemListの取得に失敗しました。");
            }
        });
    }

    protected void setItemListFlexTable(){
        int row = 0;
        checkItemListFlextable.setText(row, COLUMN_ITEMNAME, "ItemName");
        checkItemListFlextable.setText(row, COLUMN_ITEMID, "ItemId");
        checkItemListFlextable.setText(row, COLUMN_COLOR, "Color");
        checkItemListFlextable.setText(row, COLUMN_TYPE, "Type");

        row++;
        for(CheckItemList checkItemList:checkItemListList){

            TextBox itemNameTextBox = new TextBox();
            itemNameTextBox.setText(checkItemList.getItemName());
            checkItemListFlextable.setWidget(row, COLUMN_ITEMNAME, itemNameTextBox);

            TextBox itemIdTextBox = new TextBox();
            itemIdTextBox.setText(checkItemList.getItemId());
            checkItemListFlextable.setWidget(row, COLUMN_ITEMID, itemIdTextBox);

            TextBox itemColorTextBox = new TextBox();
            itemColorTextBox.setText(checkItemList.getColorId());
            checkItemListFlextable.setWidget(row, COLUMN_COLOR, itemColorTextBox);

            TextBox itemTypeTextBox = new TextBox();
            itemTypeTextBox.setText(checkItemList.getType());
            checkItemListFlextable.setWidget(row, COLUMN_TYPE, itemTypeTextBox);

            Hidden keyHidden = new Hidden();
            keyHidden.setValue(checkItemList.getKey().toString());
            checkItemListFlextable.setWidget(row, COLUMN_KEYHIDDEN, keyHidden);

            row++;
        }

    }

    private void setButton(){
        addButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                addItemList();
            }
        });

        saveButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                saveItemList();
            }
        });
    }

    protected void saveItemList(){
        setCheckItemListList = new ArrayList<CheckItemList>();
        for(int row = 1;row < checkItemListFlextable.getRowCount();row++){
            Hidden keyHidden = (Hidden)checkItemListFlextable.getWidget(row, COLUMN_KEYHIDDEN);

            CheckItemList checkItemList = null;

            if(!keyHidden.getValue().isEmpty()){
                for(CheckItemList oldCheckItemList:checkItemListList){
                    if(oldCheckItemList.getKey().toString().equals(keyHidden.getValue())){
                        checkItemList = oldCheckItemList;
                        break;
                    }
                }
            }else{
                checkItemList = new CheckItemList();
            }

            TextBox itemNameTextBox = (TextBox)checkItemListFlextable.getWidget(row, COLUMN_ITEMNAME);
            if(itemNameTextBox.getText().isEmpty()){
                continue;
            }
            checkItemList.setItemName(itemNameTextBox.getText());

            TextBox itemIdTextBox = (TextBox)checkItemListFlextable.getWidget(row, COLUMN_ITEMID);
            checkItemList.setItemId(itemIdTextBox.getText());

            TextBox itemColorTextBox = (TextBox)checkItemListFlextable.getWidget(row, COLUMN_COLOR);
            checkItemList.setColorId(itemColorTextBox.getText());

            TextBox itemTypeTextBox = (TextBox)checkItemListFlextable.getWidget(row, COLUMN_TYPE);
            checkItemList.setType(itemTypeTextBox.getText());

            setCheckItemListList.add(checkItemList);
        }

        checkItemListService.setCheckItemList(setCheckItemListList, new AsyncCallback<Void>(){

            public void onSuccess(Void result){
                setInfoMessage("CheckItemListの更新に成功しました。");
                clearErrorMessage();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("CheckItemListの更新に失敗しました。");
            }
        });
    }

    protected void addItemList(){
        int row = checkItemListFlextable.getRowCount();

        checkItemListFlextable.setWidget(row, COLUMN_ITEMNAME, new TextBox());
        checkItemListFlextable.setWidget(row, COLUMN_ITEMID, new TextBox());
        checkItemListFlextable.setWidget(row, COLUMN_COLOR, new TextBox());
        checkItemListFlextable.setWidget(row, COLUMN_TYPE, new TextBox());
        checkItemListFlextable.setWidget(row, COLUMN_KEYHIDDEN, new Hidden());
    }
}
